const crypto = require('crypto');

/**
 * 获取当前时间戳（秒）
 * @returns {string} - 当前时间戳字符串
 */
const nowDate = () => {
  return Math.floor(Date.now() / 1000).toString();
};

/**
 * 生成UUID
 * @returns {string} - UUID字符串
 */
const uuid = () => {
  return crypto.randomBytes(16).toString('hex');
};

/**
 * 格式化金额为两位小数
 * @param {number} amount - 金额
 * @returns {string} - 格式化后的金额字符串
 */
const formatAmount = (amount) => {
  return Number(amount).toFixed(2);
};

/**
 * 生成指定长度的随机字符串
 * @param {number} length - 字符串长度
 * @returns {string} - 随机字符串
 */
const randomString = (length = 32) => {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return result;
};

/**
 * 检查是否为移动设备
 * @param {string} userAgent - User-Agent字符串
 * @returns {boolean} - 是否为移动设备
 */
const isMobileDevice = (userAgent) => {
  return /mobile|android|iphone|ipad|phone/i.test(userAgent);
};

module.exports = {
  nowDate,
  uuid,
  formatAmount,
  randomString,
  isMobileDevice
};